using System;
using System.Media;
using System.Collections.Generic;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

using CueCatDecoder;

namespace AmazonLiveWriterPlugin
{
    [WriterPlugin("605EEA63-B74B-4e9d-A290-F5E9E8229FC1", "Amazon Books with CueCat",
        ImagePath = "Images.CueCat.png",
        PublisherUrl = "http://myblog4fun.com",
        Description = "Amazon Book Links using a CueCat.")]

    [InsertableContentSource("Amazon Links")]
    public class Plugin : ContentSource
    {
        const string AMAZONASSOCIATESID = "amazonassociatesid";
        const string MY_AWS_ACCESS_KEY_ID = "your aws access key goes here";
        const string MY_AWS_SECRET_KEY = "your aws private key goes here";

        public override System.Windows.Forms.DialogResult CreateContent(System.Windows.Forms.IWin32Window dialogOwner, ref string newContent)
        {
            using(InsertForm form = new InsertForm())
            {
                form.AmazonAssociatesID = this.Options[AMAZONASSOCIATESID];
                form.AmazonWebServicesID = this.Options[MY_AWS_ACCESS_KEY_ID];
                form.AmazonWebServicesSecretKey = this.Options[MY_AWS_SECRET_KEY];

                DialogResult result = form.ShowDialog();
                if(result == DialogResult.OK)
                {
                    this.Options[AMAZONASSOCIATESID] = form.AmazonAssociatesID;
                    this.Options[MY_AWS_ACCESS_KEY_ID] = form.AmazonWebServicesID;
                    this.Options[MY_AWS_SECRET_KEY] = form.AmazonWebServicesSecretKey;

                    try
                    {
                        Product p = Decoder.Decode(form.CueCatData);
                        if (p != null)
                        {
                            AmazonBook book = AmazonBookPopulator.CreateAmazonProduct(p, form.AmazonWebServicesID, form.AmazonWebServicesSecretKey);

                            string associatesId = form.AmazonAssociatesID.Trim();
                            if (associatesId == "")
                            {
                                associatesId = "diabeticbooks"; //use mine if there isn't one. That way they'll at least get the enhanced pictures and html.
                            }
                            string builtAmazonUrl = "<a href=\"http://www.amazon.com/exec/obidos/redirect?link_code=as2&path=ASIN/{0}&tag={1}&camp=1789&creative=9325\">{2} by {3}</a><a href=\"http://www.amazon.com/exec/obidos/redirect?link_code=as2&path=ASIN/{0}&tag={1}&camp=1789&creative=9325\"><img border=\"0\" src=\"http://images.amazon.com/images/P/{0}.01._AA_SCMZZZZZZZ_.jpg\"></a><img src=\"http://www.assoc-amazon.com/e/ir?t={1}&l=as2&o=1&a={0}\" width=\"1\" height=\"1\" border=\"0\" alt=\"\" style=\"border:none !important; margin:0px !important;\" />";
                            newContent = string.Format(builtAmazonUrl, book.ID, associatesId, book.Title, book.Author);
                        }
                    }
                    catch (Exception ex)
                    {
                        newContent = "";
                        MessageBox.Show("Sorry, an error occured trying to build your Amazon Url." + Environment.NewLine + ex.ToString(), "Error", MessageBoxButtons.OK);
                    }
                }
                return result;
            }
        }
    }
}
